package work.xiaohong.saolei.controller;

import com.alibaba.fastjson.JSON;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;
import work.xiaohong.saolei.pojo.SendMsg;
import work.xiaohong.saolei.pojo.User;
import work.xiaohong.saolei.service.SaoleiUserService;
import work.xiaohong.saolei.websocket.PkCentre;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author : xiaohong
 * @date : 2021/5/30 14:13
 */
@RestController
@RequestMapping("/saoleiPk")
public class PkController {

    @Autowired
    private PkCentre PkCentre;

    @Autowired
    private SaoleiUserService saoleiUserService;

    // 获取连接者消息
    @GetMapping("/getUserInfo/{userId}")
    public Result getUserInfo (@PathVariable String userId){
        return new Result(true, StatusCode.OK.getCode(),"获取连接者消息成功",PkCentre.chatCentreClients.get(userId));
    }

    // 获取连接人数
    @GetMapping("/getNumber")
    public Result getNumer() {
        return new Result(true, StatusCode.OK.getCode(),"获取连接人数成功",PkCentre.chatCentreClients.size());
    }

    /**
     * 获取指定用户连接状态
     * @param cid
     * @return 0 用户离线 1 用户在线 2 用户隐身 3用户不存在
     */
    @GetMapping("/status/{cid}")
    public Result<String> getStatus(@PathVariable Long cid){
        if (PkCentre.chatCentreClients.get(cid) != null){
            PkCentre webSocketCentre = PkCentre.chatCentreClients.get(cid);
            if(!webSocketCentre.session.isOpen()){
                return new Result<String>(true, StatusCode.OK.getCode(),"用户离线","离线");
            }else {
                return new Result<String>(true, StatusCode.OK.getCode(),"用户在线","在线");
            }
        }else {
            // 看看数据库有没有这个人的信息
            User user = null;
            try {
                user = saoleiUserService.findById(cid);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return new Result<String>(true, StatusCode.OK.getCode(),"用户不存在","注销");
            }
            if (user != null){
                // 有这人 当离线处理
                return new Result<String>(true, StatusCode.OK.getCode(),"用户隐身","离线");
            }else{
                // 用户不存在
                return new Result<String>(true, StatusCode.OK.getCode(),"用户不存在","注销");
            }
        }
    }


    /**
     * 向指定对象发送消息
     * @return
     */
    @PostMapping("/send")
    public Result sendMessage(@RequestBody SendMsg sendMsg) throws IOException {
        // 判断接收者状态
        try {
            Long from = Long.parseLong(sendMsg.getSender());
            Long to = Long.parseLong(sendMsg.getReceiver());

            if (getStatus(to).getData().equals("离线")){
                //用户离线 返回到mq
                if (getStatus(from).getData() == "在线"){
                    return new Result(true,StatusCode.ERROR.getCode(),"用户已离线",sendMsg);
                }else {
                    System.out.println("发送者离线");
                }
            }else if (getStatus(to).getData().equals("在线")){
                // 用户在线
                if (getStatus(from).getData().equals("在线")){
                    PkCentre.chatCentreClients.get(to).session.getBasicRemote().sendText(JSON.toJSONString(sendMsg));
                    return new Result(true,StatusCode.OK.getCode(),"发送成功",sendMsg);
                }else {
                    System.out.println("发送者离线");
                }
            }else if(getStatus(to).getData().equals("已注销")){
                // 用户不存在
                if (getStatus(from).getData().equals("在线")){
                    // 用户不存在
                    return new Result(true,StatusCode.ERROR.getCode(),"用户不存在",sendMsg);
                }else {
                    System.out.println("发送者离线");
                }
            }

            return new Result(true,StatusCode.ERROR.getCode(),"发送者离线",sendMsg);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true,StatusCode.ERROR.getCode(),"发送失败",sendMsg);
        }
    }
}
